package com.lyn.pluginbazaar.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    public static String getTime(long timestamp) {
        Date date = new Date();
        date.setTime(timestamp);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static long getTime(String format){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(format);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取当前星期1-7
     *
     * @param timestamp
     * @return
     */
    public static int getWeek(long timestamp) {
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        date.setTime(timestamp);
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (i < 0) {
            i = 0;
        }
        return i;
    }

    /**
     * 获取当前一个星期所有天数的开始和结束时间戳
     *
     * @param timestamp
     * @return
     */
    public static long[] getTimestampBasedOnWeek(long timestamp) {
        Date date = new Date();

        date.setTime(timestamp);
        //当前年份
        SimpleDateFormat YYYYFormat = new SimpleDateFormat("yyyy");
        int year = Integer.parseInt(YYYYFormat.format(date));
        //当前月份
        SimpleDateFormat MMFormat = new SimpleDateFormat("MM");
        int month = Integer.parseInt(MMFormat.format(date));
        //当前几号
        SimpleDateFormat ddFormat = new SimpleDateFormat("dd");
        int day = Integer.parseInt(ddFormat.format(date));
        //当前星期
        int week = getWeek(timestamp);
        //当前月天数
        int monthDays = getMonthDays(timestamp);
        int endWeek = 7 - week;
        int startWeek = 7 - endWeek;
        long[] data = new long[14];
        int startDay = day - startWeek;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 7; i++) {
            startDay += 1;
            long toDayStartTime = 0;
            long toDayEndTime = 0;
            try {
                toDayStartTime = format.parse(year + "-" + month + "-" + startDay + " " + "00:00:00").getTime();
                toDayEndTime = format.parse(year + "-" + month + "-" + startDay + " " + "23:59:59").getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            data[i * 2] = toDayStartTime;
            data[i * 2 + 1] = toDayEndTime;
        }
        return data;
    }

    /**
     * 根据时间戳获取当天开始结束时间戳
     *
     * @param timestamp
     * @return
     */
    public static long[] getStartEndEnds(long timestamp) {
        Date date = new Date();
        date.setTime(timestamp);
        //当前年份
        SimpleDateFormat YYYYFormat = new SimpleDateFormat("yyyy");
        int year = Integer.parseInt(YYYYFormat.format(date));
        //当前月份
        SimpleDateFormat MMFormat = new SimpleDateFormat("MM");
        int month = Integer.parseInt(MMFormat.format(date));
        //当前几号
        SimpleDateFormat ddFormat = new SimpleDateFormat("dd");
        int day = Integer.parseInt(ddFormat.format(date));
        long toDayStartTime = 0;
        long toDayEndTime = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String startDateStr = year + "-" + month + "-" + day + " " + "00:00:00";
            String endDateStr = year + "-" + month + "-" + day + " " + "23:59:59";
            toDayStartTime = format.parse(startDateStr).getTime();
            toDayEndTime = format.parse(endDateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new long[]{toDayStartTime, toDayEndTime};
    }


    /**
     * 根据当前时间戳计算本月多少天
     *
     * @return
     */
    public static int getMonthDays(long timestamp) {
        int year = 0;
        int month = 0;
        Date date = new Date();
        date.setTime(timestamp);
        SimpleDateFormat YYYYFormat = new SimpleDateFormat("yyyy");
        year = Integer.parseInt(YYYYFormat.format(date));
        SimpleDateFormat MMFormat = new SimpleDateFormat("MM");
        month = Integer.parseInt(MMFormat.format(date));
        if (month == 2) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                return 29;
            } else {
                return 28;
            }
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        } else {
            return 31;
        }
    }

    /**
     * 根据时间戳获取当前日
     * @param timestamp
     * @return
     */
    public static int getDay(long timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        Date date = new Date();
        date.setTime(timestamp);
        String format = simpleDateFormat.format(date);
        try {
            return Integer.parseInt(format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据时间戳获取当前月
     * @param timestamp
     * @return
     */
    public static int getMonth(long timestamp){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        Date date = new Date();
        date.setTime(timestamp);
        String format = simpleDateFormat.format(date);
        try {
            return Integer.parseInt(format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据时间戳获取当前年
     * @param timestamp
     * @return
     */
    public static int getYear(long timestamp){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY");
        Date date = new Date();
        date.setTime(timestamp);
        String format = simpleDateFormat.format(date);
        try {
            return Integer.parseInt(format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }



}
